﻿using Abide_AddOn_API.Interfaces;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Abide_AddOn_API.Controls
{
    [ToolboxItem(false), ComVisible(true), Designer(typeof(Design.SettingsPageDesigner))]
    public class SettingsPage : UserControl, ISettingsPage
    {
        private event EventHandler saveSettings;
        private event EventHandler loadSettings;
        private event EventHandler applySettings;

        [Browsable(false)]
        public new bool AutoScroll
        {
            get { return true; }
        }
        /// <summary>
        /// Gets and sets the tab text of this page.
        /// </summary>
        [Browsable(true), Category("Appearance"), Description("The display text of the options tab.")]
        public string DisplayName
        {
            get { return text; }
            set { text = value; }
        }
        /// <summary>
        /// Gets and sets the display icon of the AddOn.
        /// </summary>
        [Browsable(true), Category("Appearance"), Description("The display icon of the options tab.")]
        public Image Icon
        {
            get { return icon; }
            set { icon = value; }
        }
        /// <summary>
        /// Occurs when the settings are saved and applied.
        /// </summary>
        [Browsable(true), Category("Behavior"), Description("Occurs when the selected options are applied.")]
        public event EventHandler ApplySettings
        {
            add { applySettings += value; }
            remove { applySettings -= value; }
        }
        /// <summary>
        /// Occurs when the settings are saved.
        /// </summary>
        [Browsable(true), Category("Behavior"), Description("Occurs when the selected options are saved.")]
        public event EventHandler SaveSettings
        {
            add { saveSettings += value; }
            remove { saveSettings -= value; }
        }
        /// <summary>
        /// Occurs when the settings are loaded.
        /// </summary>
        [Browsable(true), Category("Behavior"), Description("Occurs when the selected options are loaded into the control.")]
        public event EventHandler LoadSettings
        {
            add { loadSettings += value; }
            remove { loadSettings -= value; }
        }

        UserControl ISettingsPage.Interface
        {
            get { return this; }
        }
        Image ISettingsPage.Icon
        {
            get { return icon; }
        }
        string ISettingsPage.Text
        {
            get { return text; }
        }
        private string text = "SettingsPage";
        private Image icon = null;

        protected SettingsPage()
        {
            //Set
            base.AutoScroll = true;
            base.BackColor = Color.White;
        }
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            if (DesignMode)
                base.SetBoundsCore(x, y, 388, height, BoundsSpecified.Width);
            else
                base.SetBoundsCore(x, y, width, height, specified);
        }
        protected virtual void OnLoadSettings(EventArgs e)
        {
            //Throw Event
            if (loadSettings != null)
                loadSettings(this, e);
        }
        protected virtual void OnSaveSettings(EventArgs e)
        {
            //Throw Event
            if (saveSettings != null)
                saveSettings(this, e);
        }
        protected virtual void OnApplySettings(EventArgs e)
        {
            //Throw Event
            if (applySettings != null)
                applySettings(this, e);
        }
        void ISettingsPage.LoadSettings()
        {
            OnLoadSettings(new EventArgs());
        }
        void ISettingsPage.SaveSettings()
        {
            OnSaveSettings(new EventArgs());
        }
        void ISettingsPage.ApplySettings()
        {
            OnApplySettings(new EventArgs());
        }
        void IDisposable.Dispose()
        {
            base.Dispose();
        }
    }
}